package com.timber.util;

import javax.swing.table.*;
import pv.jfcx.*;

/**
 *
 * class to define table column properties
 * allows to set column to one of the JPV types, custom editor, renderer
 * editable flag and edit string
 *
 * also provides support for applying functions, SUM or AVERAGE, to column values
 * by setting func property and using GenericTableView to display the data,
 * you will get a totals row
 * on the buttom of the screen that is fully synced up with the viewable area.
 */
public class SLColumnDefinition implements Cloneable
{
  public static final int FUNCTION_NONE		= -1;
  public static final int FUNCTION_SUM      	= 1;
  public static final int FUNCTION_AVERAGE      = 2;

  protected int			id;
  protected String		idName;
  protected String		displayLabel;
  protected String		colEditString;
  protected int			colType;	//	JPV Column Type
  protected PVTableRenderer	renderer;
  protected TableCellEditor	editor;
  protected int			displayWidth;
  protected boolean		editable;
  protected int			sortPos = -1;
  protected boolean		sortAscending = true;
  protected boolean		visible = true;
  protected int        		function = FUNCTION_NONE;
  protected boolean    		isInDB	= true;
  protected String 		headerToolTip = null;

  public SLColumnDefinition( int		id,
			     String		idName,
			     String		displayLabel,
			     int		colType,
			     PVTableRenderer	renderer,
			     TableCellEditor	editor,
			     int		displayWidth
			     )
  {
    this.id = id;
    this.idName = idName;
    this.displayLabel = new String( displayLabel );
    this.renderer = renderer;
    this.displayWidth = displayWidth;
    this.editor = editor;
    this.colType = colType;

    if( displayLabel != null )
    {
      colEditString = new String( displayLabel );
      colEditString = colEditString.replace( '\n', ' ' );
      colEditString = colEditString.trim();
    }
  }

  public SLColumnDefinition( int	id,
			     String	idName,
			     String	displayLabel,
			     int	colType,
			     int	displayWidth
			     )
  {
    this( id, idName, displayLabel, colType, null, null, displayWidth );
  }

  public SLColumnDefinition( int		id,
			     String		idName,
			     String		displayLabel,
			     PVTableRenderer	renderer,
			     TableCellEditor	editor,
			     int		displayWidth
			     )
  {
    this( id, idName, displayLabel, -1, renderer, editor, displayWidth );
  }

  public SLColumnDefinition( int		id,
			     String		idName,
			     String		displayLabel,
			     PVTableRenderer	renderer,
			     TableCellEditor	editor,
			     int		displayWidth,
			     boolean		editable
			     )
  {
    this( id, idName, displayLabel, -1, renderer, editor, displayWidth );
    this.editable = editable;
  }

  public SLColumnDefinition( int		id,
			     String		idName,
			     String		displayLabel,
			     PVTableRenderer	renderer,
			     TableCellEditor	editor,
			     int		displayWidth,
			     int		function
			     )
  {

    this( id, idName, displayLabel, -1, renderer, editor, displayWidth );
    this.function = function;
  }

  public Object clone() throws CloneNotSupportedException
  {
    return super.clone();
  }


  public int getId()
  {
    return id;
  }

  public void setId( int id )
  {
    this.id = id;
  }

  public String getIdName()
  {
    return idName;
  }

  public void setIdName( String idName )
  {
    this.idName = idName;
  }


  public String getDisplayLabel()
  {
    return new String( displayLabel );
  }

  public void setDisplayLabel( String displayLabel )
  {
    this.displayLabel = new String( displayLabel );
  }

  public void setDisplayLabelAndColEditString( String displayLabel )
  {
    this.displayLabel = new String( displayLabel );

    if( displayLabel != null )
    {
      colEditString = new String( displayLabel );
      colEditString = colEditString.replace( '\n', ' ' );
      colEditString = colEditString.trim();
    }
  }

  public String getColEditString()
  {
    return colEditString;
  }

  public void setColEditString( String colEditString )
  {
    this.colEditString = new String( colEditString );
  }

  public PVTableRenderer getRenderer()
  {
    return renderer;
  }

  public void setRenderer( PVTableRenderer renderer )
  {
    this.renderer = renderer;
  }


  public int getDisplayWidth()
  {
    return displayWidth;
  }

  public void setDisplayWidth( int displayWidth )
  {
    this.displayWidth = displayWidth;
  }


  public TableCellEditor getEditor()
  {
    return editor;
  }

  public void setEditor( TableCellEditor editor )
  {
    this.editor = editor;
  }

  /**
   * return column data type, one of PV types
   * if not set, get from renderer
   * @return
   */
  public int getColType()
  {
    if( colType < 0 && renderer != null  )
      return renderer.getType();

    return colType;
  }

  public void setColType( int id )
  {
    this.colType = id;
  }

  public void setEditable( boolean b )
  {
    editable = b;
  }

  public boolean isEditable()
  {
    return editable;
  }

  public String toString()
  {
    return colEditString;
  }

  public int getSortPos()
  {
    return sortPos;
  }

  public void setSortPos( int sortPos )
  {
    this.sortPos = sortPos;
  }

  public boolean getSortAscending()
  {
    return sortAscending;
  }

  public void setSortAscending( boolean sortAscending )
  {
    this.sortAscending = sortAscending;
  }

  public boolean equals( Object o )
  {
    SLColumnDefinition cd = ( SLColumnDefinition )o;
    if( this.id == cd.id )
      return true;
    else
      return false;
  }

  public boolean isVisible()
  {
    return visible;
  }

  public void setVisible( boolean b )
  {
    visible = b;
  }

  public int getColumnFunction()
  {
    return function;
  }

  public void setColumnFunction( int i )
  {
    function = i;
  }

  public boolean isInResSet()
  {
    return isInDB;
  }

  public void setHeaderToolTip( String s )
  {
    headerToolTip = s;
  }

  /**
   * return toolTip when:
   * --  explicitly set in ColumnDefinition
   * --  column with a function
   *
   * called from SLTableModel.getHeaderToolTip()
   *
   * @return String
   */
  public String getHeaderToolTip()
  {
    if( headerToolTip != null )
      return headerToolTip;
    else
      /*if( function > 0 )
      {
        switch( function )
        {
          case FUNCTION_SUM:
            return getDisplayLabel() + "  Sum in Totals Panel";
          case FUNCTION_AVERAGE:
            return getDisplayLabel() + "  Average in Total Panel";
        }
      }*/

    return null;
  }
}
